package admin

import (
	"encoding/json"
	"lphoto/models"
	"lphoto/utils"
	"lphoto/validations/adminValid"
)

type LoginController struct {
	BaseController
}

func (c *LoginController) Index() {
	c.TplName = "admin/login/login.tpl"
}

func (c *LoginController) Logout()  {
	c.DelSession("userInfo")
	c.Redirect("/admin/login",302)
}

func (c *LoginController) DoLogin() {
	u := adminValid.LoginValidation{}

	if err := json.Unmarshal(c.Ctx.Input.RequestBody, &u); err != nil {
		c.responseFail(err.Error(),"")
	}
	result, msg := u.ValidatorAuto(&u)
	if !result {
		c.responseFail(msg,"")
	}

	userInfo := models.GetUserInfoForUsername(u.Username)
	if userInfo == nil {
		c.responseFail("用户不存在","")
	}
	if userInfo.Password != utils.MD5(u.Password) {
		c.responseFail("密码错误","")
	}
	c.SetSession("userInfo", userInfo)
	c.responseJson(1,"登陆成功", userInfo)
}